package com.itboye.mengwu.data.repository;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.utils.BaseErrorListener;
import com.itboye.core.utils.BaseSuccessReqListener;
import com.itboye.core.utils.ByJsonRequest.Builder;
import com.itboye.mengwu.data.interfaces.IShopCar;
import com.itboye.mengwu.data.model.AddShopCartSuccessBean;
import com.itboye.mengwu.data.model.ShopCarBean;
import com.itboye.mengwu.data.model.ShopCarBeanCopy;

/***
 * 购物车相关接口
 * 
 * @author admin
 * 
 */
public class ShopCarResitory extends BaseNetRepository implements
		IShopCar<ShopCarBean> {

	public static final String ShopCartAdd = "BY_ShoppingCart_add";// 7.1 添加商品
	public static final String ShopCartUpdate = "BY_ShoppingCart_edit";// 7.2修改商品
	public static final String ShopCartdelete = "BY_ShoppingCart_delete";// 7.3删除商品
	public static final String ShopCartCount = "BY_ShoppingCart_count";// 7.4
																		// 商品库存
	public static final String ShopCartQuerySingle = "BY_ShoppingCart_getInfo";// 7.5查看单个
	public static final String ShopCartQuery = "BY_ShoppingCart_query";// 7.6分页查询
	public static final String ShopCartAllQuery = "BY_ShoppingCart_all";// 全部查询
	String api = "100";

	public ShopCarResitory(ICompleteListener listener) {
		super(listener);
	}

	/**
	 * 添加购物车
	 */
	@Override
	public void add(ShopCarBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<AddShopCartSuccessBean>() {
		}.getType();
		String apiVer = "101";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", model.getUid());
		params.put("pid", model.getId());
		params.put("count", model.getCount());
		params.put("psku_id", model.getPskuId());
		(new Builder<AddShopCartSuccessBean>())
				.setTypeVerParamsAndReturnClass(ShopCartAdd, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<AddShopCartSuccessBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	/***
	 * 购物车单个删除
	 * 
	 * @Override
	 */
	public void delete(ShopCarBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", model.getUid());
		params.put("id", model.getId());
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ShopCartdelete, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	/**
	 * 7.2购物车修改商品
	 */
	@Override
	public void update(ShopCarBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", model.getId());// 购物车id,若批量 用','隔开 如1,3,5
		params.put("count", model.getCount());// 数量需与id一一对应若批量 用','隔开 如1,3,5
		params.put("uid", model.getUid());
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ShopCartUpdate, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	/***
	 * 7.4 查看商品库存
	 */
	@Override
	public void getInfo(ShopCarBean model) {
		// TODO Auto-generated method stubpsku_id
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("psku_id", model.getSkuId());// 规格Id
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ShopCartCount, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public List<ShopCarBean> query(ShopCarBean model) {
		// TODO Auto-generated method stub
		return null;
	}

	/***
	 * 7.6购物车分页查询
	 */
	@Override
	public void getShopcar(String uid) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<ShopCarBeanCopy>() {
		}.getType();
		String apiVer = "101";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		(new Builder<ShopCarBeanCopy>())
				.setTypeVerParamsAndReturnClass(ShopCartAllQuery, apiVer,
						params, type)
				.requestListener(
						new BaseSuccessReqListener<ShopCarBeanCopy>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	/***
	 * 7.3购物车删除商品 （多个）
	 */
	@Override
	public void deleteShopcar(StringBuilder id) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ShopCartdelete, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}
}
